package apptoko.com;

import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

import android.os.Environment;
import android.preference.PreferenceManager;
import apptoko.com.util.Constants;
import apptoko.com.util.StorageUtils;

import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.BitmapAjaxCallback;
import com.androidquery.util.AQUtility;

public class AppStoreVnApplication extends UILApplication {

	private String currentStore;
	private String accessToken;
	private boolean isLogin;
	private boolean isLoginFaceBook;
	private boolean isRooted;
	private String countryCode;
	private static Hashtable<String, String> downloadManager=new Hashtable<String, String>();
	@Override
	public void onCreate() {
		super.onCreate();
		//set the max number of concurrent network connections, default is 4
        AjaxCallback.setNetworkLimit(8);

        //set the max number of icons (image width <= 50) to be cached in memory, default is 20
        BitmapAjaxCallback.setIconCacheLimit(20);

        //set the max number of images (image width > 50) to be cached in memory, default is 20
        BitmapAjaxCallback.setCacheLimit(40);

        //set the max size of an image to be cached in memory, default is 1600 pixels (ie. 400x400)
        BitmapAjaxCallback.setPixelLimit(600 * 600);
        
        //set the max size of the memory cache, default is 1M pixels (4MB)
        BitmapAjaxCallback.setMaxPixelLimit(4000000);
        
        File ext = Environment.getExternalStorageDirectory();
        File cacheDir = new File(ext, "/AppStoreVn/caches");
        AQUtility.setCacheDir(cacheDir);

		try {
			StorageUtils.mkdir();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//fix error preferrence
		  try {
		   Boolean autoInstall = getSharedPreferences(Constants.SHARED_NAME, MODE_PRIVATE).getBoolean(SettingsActivity.KEY_AUTO_INSTALL, false);
		  } catch (Exception e) {
		   // TODO: handle exception\
			  getSharedPreferences(Constants.SHARED_NAME, MODE_PRIVATE).edit().putBoolean(SettingsActivity.KEY_AUTO_INSTALL,
		     false)
		   .commit();
		  }
	}

	public String getCurrentStore() {
		return currentStore;
	}

	public void setCurrentStore(String currentStore) {
		this.currentStore = currentStore;
	}

	public String getAccessToken() {
		return accessToken;
	}

	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}

	public boolean isLogin() {
		return isLogin;
	}

	public void setLogin(boolean isLogin) {
		this.isLogin = isLogin;
	}

	public boolean isRooted() {
		return isRooted;
	}

	public void setRooted(boolean isRooted) {
		this.isRooted = isRooted;
	}

	public boolean enableTor=false;
	
	public boolean isLoginFaceBook() {
		return isLoginFaceBook;
	}

	public void setLoginFaceBook(boolean isLoginFaceBook) {
		this.isLoginFaceBook = isLoginFaceBook;
	}
	
	public String getCountryCode() {
		return countryCode;
	}

	public void setCountryCode(String countryCode) {
		this.countryCode = countryCode;
	}
	
	public static void addDownload(String filename,String packagename){
		downloadManager.put(filename, packagename);
	}
	
	public static String getdownloadPackageName(String filename){
		return downloadManager.get(filename);
	}
	
	@Override
    public void onLowMemory(){  
        //clear all memory cached images when system is in low memory
        //note that you can configure the max image cache count, see CONFIGURATION
        BitmapAjaxCallback.clearCache();
    }

}
